package com.bq;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static int getAge(Calendar target){
        int age=0;
        Calendar instance=Calendar.getInstance();
        if(instance.before(target)){
            throw new IllegalArgumentException("参数不正确");
        }
        int day_year=instance.get(Calendar.YEAR);
        int day_Month  = instance.get(Calendar.MONTH);
        int day_day = instance.get(Calendar.DAY_OF_MONTH);
        int target_year = target.get(Calendar.YEAR);
        int target_Month = target.get(Calendar.MONTH);
        int target_day = target.get(Calendar.DAY_OF_MONTH);
        age=day_year - target_year;
        if(day_Month<=target_Month){
            if(day_Month==target_Month){
                if(day_day<target_day){
                    age--;
                }else{
                    age--;
                }
            }
        }
        return age;
    }
    public static Integer getAge(String birthday){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
            Date parse1 = simpleDateFormat.parse(birthday);
        } catch (ParseException e) {
            System.err.print("参数格式有误");
            return null;
        }
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int age = DateUtils.getAge(instance);
        return age;
    }

    public static int getFutrueDays(Calendar target){
        int day=0;
        long time = Calendar.getInstance().getTime().getTime();
        long time1 = target.getTime().getTime();
        if(time1-time<0){
           throw new IllegalArgumentException("参数不合法异常");
        }else {
            day=(int) ((time1-time)/1000/60/60/24);
        }
        return day;
    }

    public static int getFutrueDays(String date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
            Date parse1 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            new IllegalArgumentException("日期格式不正确");
        }
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int futrueDays = getFutrueDays(instance);
        return futrueDays;
    }
}
